const app = getApp()
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    url: '',
    txt: ''
  },
  get: function (ev) {
    this.setData({
      url: ev.detail.userInfo.avatarUrl,
      txt: ev.detail.userInfo.nickName
    })
  },
  // 初始化若缓存内有用户信息则不显示登录按钮
  attached: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: app.globalData.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  methods: {
    //事件处理函数
    bindViewTap: function () {
      wx.navigateTo({
        url: '../logs/logs'
      })
    },
    // 获取用户信息
    getUserInfo: function (e) {
      app.globalData.userInfo = e.detail.userInfo
      wx.setStorageSync('userInfo', app.globalData.userInfo);
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    }
  },

})
